﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Maticsoft.DBUtility;
using System.Data;
using System.Data.OracleClient;
using DQYT.HTDZ.Model;
using DQYT.HTDZ.IDAL;

namespace DQYT.HTDZ.OracleDAL
{
    public class BDUserDAL : IBDUser
    {
        public BDUserDAL()
		{

        }

		#region  Method

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int USE_ID)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select count(1) from tb_sys_user");
			strSql.Append(" where USE_ID=:USE_ID ");
			OracleParameter[] parameters = {
					new OracleParameter(":USE_ID", OracleType.Int32)			};
			parameters[0].Value = USE_ID;

			return DbHelperOra.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(BDUserModel model)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("insert into tb_sys_user(");
			strSql.Append("USE_CODE,USE_NAME,USE_ID,USE_BH,USE_PASSWORD,USE_EMAIL,USE_PHONE,BUM_ID,JSE_ID,USE_IPADRESS,USE_MACDRESS,USE_LASTTIME,USE_ZT,USE_BZ)");
			strSql.Append(" values (");
			strSql.Append(":USE_CODE,:USE_NAME,:USE_ID,:USE_BH,:USE_PASSWORD,:USE_EMAIL,:USE_PHONE,:BUM_ID,:JSE_ID,:USE_IPADRESS,:USE_MACDRESS,:USE_LASTTIME,:USE_ZT,:USE_BZ)");
			OracleParameter[] parameters = {
					new OracleParameter(":USE_CODE", OracleType.VarChar,10),
					new OracleParameter(":USE_NAME", OracleType.VarChar,20),
					new OracleParameter(":USE_ID", OracleType.Int32),
					new OracleParameter(":USE_BH", OracleType.VarChar,20),
					new OracleParameter(":USE_PASSWORD", OracleType.VarChar,20),
					new OracleParameter(":USE_EMAIL", OracleType.VarChar,50),
					new OracleParameter(":USE_PHONE", OracleType.VarChar,20),
					new OracleParameter(":BUM_ID", OracleType.Int32),
					new OracleParameter(":JSE_ID", OracleType.Int32),
					new OracleParameter(":USE_IPADRESS", OracleType.VarChar,20),
					new OracleParameter(":USE_MACDRESS", OracleType.VarChar,50),
					new OracleParameter(":USE_LASTTIME", OracleType.VarChar,30),
					new OracleParameter(":USE_ZT", OracleType.Int32),
					new OracleParameter(":USE_BZ", OracleType.VarChar,100)};
			parameters[0].Value = model.USE_CODE;
			parameters[1].Value = model.USE_NAME;
			parameters[2].Value = model.USE_ID;
			parameters[3].Value = model.USE_BH;
			parameters[4].Value = model.USE_PASSWORD;
			parameters[5].Value = model.USE_EMAIL;
			parameters[6].Value = model.USE_PHONE;
			parameters[7].Value = model.BUM_ID;
			parameters[8].Value = model.JSE_ID;
			parameters[9].Value = model.USE_IPADRESS;
			parameters[10].Value = model.USE_MACDRESS;
			parameters[11].Value = model.USE_LASTTIME;
			parameters[12].Value = model.USE_ZT;
			parameters[13].Value = model.USE_BZ;

			int rows=DbHelperOra.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(BDUserModel model)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("update tb_sys_user set ");
			strSql.Append("USE_CODE=:USE_CODE,");
			strSql.Append("USE_NAME=:USE_NAME,");
			strSql.Append("USE_BH=:USE_BH,");
			strSql.Append("USE_PASSWORD=:USE_PASSWORD,");
			strSql.Append("USE_EMAIL=:USE_EMAIL,");
			strSql.Append("USE_PHONE=:USE_PHONE,");
			strSql.Append("BUM_ID=:BUM_ID,");
			strSql.Append("JSE_ID=:JSE_ID,");
			strSql.Append("USE_IPADRESS=:USE_IPADRESS,");
			strSql.Append("USE_MACDRESS=:USE_MACDRESS,");
			strSql.Append("USE_LASTTIME=:USE_LASTTIME,");
			strSql.Append("USE_ZT=:USE_ZT,");
			strSql.Append("USE_BZ=:USE_BZ");
			strSql.Append(" where USE_ID=:USE_ID ");
			OracleParameter[] parameters = {
					new OracleParameter(":USE_CODE", OracleType.VarChar,10),
					new OracleParameter(":USE_NAME", OracleType.VarChar,20),
					new OracleParameter(":USE_BH", OracleType.VarChar,20),
					new OracleParameter(":USE_PASSWORD", OracleType.VarChar,20),
					new OracleParameter(":USE_EMAIL", OracleType.VarChar,50),
					new OracleParameter(":USE_PHONE", OracleType.VarChar,20),
					new OracleParameter(":BUM_ID", OracleType.Int32),
					new OracleParameter(":JSE_ID", OracleType.Int32),
					new OracleParameter(":USE_IPADRESS", OracleType.VarChar,20),
					new OracleParameter(":USE_MACDRESS", OracleType.VarChar,50),
					new OracleParameter(":USE_LASTTIME", OracleType.VarChar,30),
					new OracleParameter(":USE_ZT", OracleType.Int32),
					new OracleParameter(":USE_BZ", OracleType.VarChar,100),
					new OracleParameter(":USE_ID", OracleType.Int32)};
			parameters[0].Value = model.USE_CODE;
			parameters[1].Value = model.USE_NAME;
			parameters[2].Value = model.USE_BH;
			parameters[3].Value = model.USE_PASSWORD;
			parameters[4].Value = model.USE_EMAIL;
			parameters[5].Value = model.USE_PHONE;
			parameters[6].Value = model.BUM_ID;
			parameters[7].Value = model.JSE_ID;
			parameters[8].Value = model.USE_IPADRESS;
			parameters[9].Value = model.USE_MACDRESS;
			parameters[10].Value = model.USE_LASTTIME;
			parameters[11].Value = model.USE_ZT;
			parameters[12].Value = model.USE_BZ;
			parameters[13].Value = model.USE_ID;

			int rows=DbHelperOra.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int USE_ID)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("delete from tb_sys_user ");
			strSql.Append(" where USE_ID=:USE_ID ");
			OracleParameter[] parameters = {
					new OracleParameter(":USE_ID", OracleType.Number,22)			};
			parameters[0].Value = USE_ID;

			int rows=DbHelperOra.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string USE_IDlist )
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("delete from tb_sys_user ");
			strSql.Append(" where USE_ID in ("+USE_IDlist + ")  ");
			int rows=DbHelperOra.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public BDUserModel GetModel(int USE_ID)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select USE_CODE,USE_NAME,USE_ID,USE_BH,USE_PASSWORD,USE_EMAIL,USE_PHONE,BUM_ID,JSE_ID,USE_IPADRESS,USE_MACDRESS,USE_LASTTIME,USE_ZT,USE_BZ from tb_sys_user ");
			strSql.Append(" where USE_ID=:USE_ID ");
			OracleParameter[] parameters = {
					new OracleParameter(":USE_ID", OracleType.Number,22)			};
			parameters[0].Value = USE_ID;

			BDUserModel model = new BDUserModel();
			DataSet ds=DbHelperOra.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["USE_CODE"]!=null && ds.Tables[0].Rows[0]["USE_CODE"].ToString()!="")
				{
					model.USE_CODE=ds.Tables[0].Rows[0]["USE_CODE"].ToString();
				}
				if(ds.Tables[0].Rows[0]["USE_NAME"]!=null && ds.Tables[0].Rows[0]["USE_NAME"].ToString()!="")
				{
					model.USE_NAME=ds.Tables[0].Rows[0]["USE_NAME"].ToString();
				}
				if(ds.Tables[0].Rows[0]["USE_ID"]!=null && ds.Tables[0].Rows[0]["USE_ID"].ToString()!="")
				{
					model.USE_ID=int.Parse(ds.Tables[0].Rows[0]["USE_ID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["USE_BH"]!=null && ds.Tables[0].Rows[0]["USE_BH"].ToString()!="")
				{
					model.USE_BH=ds.Tables[0].Rows[0]["USE_BH"].ToString();
				}
				if(ds.Tables[0].Rows[0]["USE_PASSWORD"]!=null && ds.Tables[0].Rows[0]["USE_PASSWORD"].ToString()!="")
				{
					model.USE_PASSWORD=ds.Tables[0].Rows[0]["USE_PASSWORD"].ToString();
				}
				if(ds.Tables[0].Rows[0]["USE_EMAIL"]!=null && ds.Tables[0].Rows[0]["USE_EMAIL"].ToString()!="")
				{
					model.USE_EMAIL=ds.Tables[0].Rows[0]["USE_EMAIL"].ToString();
				}
				if(ds.Tables[0].Rows[0]["USE_PHONE"]!=null && ds.Tables[0].Rows[0]["USE_PHONE"].ToString()!="")
				{
					model.USE_PHONE=ds.Tables[0].Rows[0]["USE_PHONE"].ToString();
				}
				if(ds.Tables[0].Rows[0]["BUM_ID"]!=null && ds.Tables[0].Rows[0]["BUM_ID"].ToString()!="")
				{
					model.BUM_ID=int.Parse(ds.Tables[0].Rows[0]["BUM_ID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["JSE_ID"]!=null && ds.Tables[0].Rows[0]["JSE_ID"].ToString()!="")
				{
                    model.JSE_ID = int.Parse(ds.Tables[0].Rows[0]["JSE_ID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["USE_IPADRESS"]!=null && ds.Tables[0].Rows[0]["USE_IPADRESS"].ToString()!="")
				{
					model.USE_IPADRESS=ds.Tables[0].Rows[0]["USE_IPADRESS"].ToString();
				}
				if(ds.Tables[0].Rows[0]["USE_MACDRESS"]!=null && ds.Tables[0].Rows[0]["USE_MACDRESS"].ToString()!="")
				{
					model.USE_MACDRESS=ds.Tables[0].Rows[0]["USE_MACDRESS"].ToString();
				}
				if(ds.Tables[0].Rows[0]["USE_LASTTIME"]!=null && ds.Tables[0].Rows[0]["USE_LASTTIME"].ToString()!="")
				{
					model.USE_LASTTIME=ds.Tables[0].Rows[0]["USE_LASTTIME"].ToString();
				}
				if(ds.Tables[0].Rows[0]["USE_ZT"]!=null && ds.Tables[0].Rows[0]["USE_ZT"].ToString()!="")
				{
                    model.USE_ZT = int.Parse(ds.Tables[0].Rows[0]["USE_ZT"].ToString());
				}
				if(ds.Tables[0].Rows[0]["USE_BZ"]!=null && ds.Tables[0].Rows[0]["USE_BZ"].ToString()!="")
				{
					model.USE_BZ=ds.Tables[0].Rows[0]["USE_BZ"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

        public BDUserModel GetModel(string USE_CODE)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select USE_CODE,USE_NAME,USE_ID,USE_BH,USE_PASSWORD,USE_EMAIL,USE_PHONE,BUM_ID,JSE_ID,USE_IPADRESS,USE_MACDRESS,USE_LASTTIME,USE_ZT,USE_BZ from tb_sys_user ");
            strSql.Append(" where USE_CODE=:USE_CODE ");
            OracleParameter[] parameters = {
					new OracleParameter(":USE_CODE", OracleType.VarChar,20)			};
            parameters[0].Value = USE_CODE;

            BDUserModel model = new BDUserModel();
            DataSet ds = DbHelperOra.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["USE_CODE"] != null && ds.Tables[0].Rows[0]["USE_CODE"].ToString() != "")
                {
                    model.USE_CODE = ds.Tables[0].Rows[0]["USE_CODE"].ToString();
                }
                if (ds.Tables[0].Rows[0]["USE_NAME"] != null && ds.Tables[0].Rows[0]["USE_NAME"].ToString() != "")
                {
                    model.USE_NAME = ds.Tables[0].Rows[0]["USE_NAME"].ToString();
                }
                if (ds.Tables[0].Rows[0]["USE_ID"] != null && ds.Tables[0].Rows[0]["USE_ID"].ToString() != "")
                {
                    model.USE_ID = int.Parse(ds.Tables[0].Rows[0]["USE_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["USE_BH"] != null && ds.Tables[0].Rows[0]["USE_BH"].ToString() != "")
                {
                    model.USE_BH = ds.Tables[0].Rows[0]["USE_BH"].ToString();
                }
                if (ds.Tables[0].Rows[0]["USE_PASSWORD"] != null && ds.Tables[0].Rows[0]["USE_PASSWORD"].ToString() != "")
                {
                    model.USE_PASSWORD = ds.Tables[0].Rows[0]["USE_PASSWORD"].ToString();
                }
                if (ds.Tables[0].Rows[0]["USE_EMAIL"] != null && ds.Tables[0].Rows[0]["USE_EMAIL"].ToString() != "")
                {
                    model.USE_EMAIL = ds.Tables[0].Rows[0]["USE_EMAIL"].ToString();
                }
                if (ds.Tables[0].Rows[0]["USE_PHONE"] != null && ds.Tables[0].Rows[0]["USE_PHONE"].ToString() != "")
                {
                    model.USE_PHONE = ds.Tables[0].Rows[0]["USE_PHONE"].ToString();
                }
                if (ds.Tables[0].Rows[0]["BUM_ID"] != null && ds.Tables[0].Rows[0]["BUM_ID"].ToString() != "")
                {
                    model.BUM_ID = int.Parse(ds.Tables[0].Rows[0]["BUM_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["JSE_ID"] != null && ds.Tables[0].Rows[0]["JSE_ID"].ToString() != "")
                {
                    model.JSE_ID = int.Parse(ds.Tables[0].Rows[0]["JSE_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["USE_IPADRESS"] != null && ds.Tables[0].Rows[0]["USE_IPADRESS"].ToString() != "")
                {
                    model.USE_IPADRESS = ds.Tables[0].Rows[0]["USE_IPADRESS"].ToString();
                }
                if (ds.Tables[0].Rows[0]["USE_MACDRESS"] != null && ds.Tables[0].Rows[0]["USE_MACDRESS"].ToString() != "")
                {
                    model.USE_MACDRESS = ds.Tables[0].Rows[0]["USE_MACDRESS"].ToString();
                }
                if (ds.Tables[0].Rows[0]["USE_LASTTIME"] != null && ds.Tables[0].Rows[0]["USE_LASTTIME"].ToString() != "")
                {
                    model.USE_LASTTIME = ds.Tables[0].Rows[0]["USE_LASTTIME"].ToString();
                }
                if (ds.Tables[0].Rows[0]["USE_ZT"] != null && ds.Tables[0].Rows[0]["USE_ZT"].ToString() != "")
                {
                    model.USE_ZT = int.Parse(ds.Tables[0].Rows[0]["USE_ZT"].ToString());
                }
                if (ds.Tables[0].Rows[0]["USE_BZ"] != null && ds.Tables[0].Rows[0]["USE_BZ"].ToString() != "")
                {
                    model.USE_BZ = ds.Tables[0].Rows[0]["USE_BZ"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select USE_CODE,USE_NAME,USE_ID,USE_BH,USE_PASSWORD,USE_EMAIL,USE_PHONE,BUM_ID,JSE_ID,USE_IPADRESS,USE_MACDRESS,USE_LASTTIME,USE_ZT,USE_BZ ");
			strSql.Append(" FROM tb_sys_user ");
            strSql.Append(" where jse_id = 7 ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" and "+strWhere);
			}
			return DbHelperOra.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM tb_sys_user ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            object obj = DbHelperOra.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.USE_ID desc");
			}
			strSql.Append(")AS Row, T.*  from tb_sys_user T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperOra.Query(strSql.ToString());
		}

		#endregion  Method

        #region 新添加
        /// <summary>
        /// 判断用户名密码
        /// </summary>
        /// <param name="UserCode">用户代码</param>
        /// <param name="Pwd">登录密码</param>
        /// <returns>1为成功，0为密码错误，-1用户名错误</returns>
        public int CheckUserAndPwd(string UserCode, string Pwd)
        {
            #region
            BDUserModel User = GetModel(UserCode);
            if (User != null)
            {
                if (User.USE_PASSWORD.ToLower().Trim().Equals(Pwd.ToLower().Trim()))
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return -1;
            }

            #endregion
        }

        /// <summary>
        /// 更新密码
        /// </summary>
        /// <param name="USE_ID">用户ID</param>
        /// <param name="Pwd">用户密码</param>
        /// <returns></returns>
        public bool UpdatePwd(int USE_ID, string Pwd)
        {
            #region 
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tb_sys_user set ");
            strSql.Append("USE_PASSWORD='"+Pwd+"' WHERE USE_ID=" + USE_ID.ToString());
            int iNum= DbHelperOra.ExecuteSql(strSql.ToString());
            if(iNum>0)
            {
                return true;
            }
            return false;
            #endregion
        }

        /// <summary>
        /// 获得查询结果
        /// </summary>
        /// <param name="strWhere">查询条件 from userinfo u,jiaose j,bumen b</param>
        /// <returns>select u.*,b.bum_mc,j.jse_name</returns>
        public DataTable GetBendData(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select u.*,b.bum_mc,j.jse_name from tb_sys_user u left JOIN tb_sys_role j ON u.jse_id=j.jse_id left JOIN  tb_sys_department b ON u.bum_id=b.bum_id ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where (" + strWhere + ")");
            }
            return DbHelperOra.Query(strSql.ToString()).Tables[0];
        }

        /// <summary>
        /// 检查用户代码是否存在
        /// </summary>
        /// <param name="USE_CODE">用户代码</param>
        /// <returns></returns>
        public bool CheckCode(string USE_CODE)
        {
            #region 
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select USE_ID from tb_sys_user WHERE USE_CODE='"+USE_CODE+"'");

            object obj= DbHelperOra.GetSingle(strSql.ToString());
            if(Convert.ToInt32(obj)>0)
            {
                return true ;
            }
            return false;
            #endregion
        }

        /// <summary>
        /// 检查用户代码是否存在
        /// </summary>
        /// <param name="USE_CODE">用户代码</param>
        /// <param name="Cur_USE_ID">当前用户ID</param>
        /// <returns></returns>
        public bool CheckCode(string USE_CODE,string Cur_USE_ID)
        {
            #region
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select USE_ID from tb_sys_user WHERE USE_CODE='" + USE_CODE + "' AND USE_ID<>" + Cur_USE_ID);

            object obj = DbHelperOra.GetSingle(strSql.ToString());
            if (Convert.ToInt32(obj) > 0)
            {
                return true;
            }
            return false;
            #endregion
        }

        /// <summary>
        /// 获得下一个ID
        /// </summary>
        /// <returns></returns>
        public int GetNextID()
        {
            return DbHelperOra.GetMaxID("USE_ID", "tb_sys_user") ;
        }

        /// <summary>
        /// 验证用户
        /// </summary>
        /// <param name="USE_ID">用户ID</param>
        /// <param name="MNU_ID">菜单ID</param>
        /// <returns></returns>
        public bool VerifyUser(int USE_ID, int MNU_ID)
        {
            #region 
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM tb_sys_user u , tb_sys_role_menu jc WHERE  u.jse_id=jc.jse_id ");
            strSql.Append(" and u.use_id ="+USE_ID.ToString() +" and jc.mnu_id=" + MNU_ID.ToString());

            DataTable dt = DbHelperOra.Query(strSql.ToString()).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                return true;
            }
            return false;
            #endregion
        }

        /// <summary>
        /// 获得用户列表
        /// </summary>
        /// <param name="JiaoSe">较色名</param>
        /// <returns></returns>
        public DataTable GetUserList(string JiaoSe)
        {
            #region 
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  u.* from tb_sys_user u ,tb_sys_role j where u.jse_id=j.jse_id and j.jse_name='"+JiaoSe+"' ");

            DataTable dt = DbHelperOra.Query(strSql.ToString()).Tables[0];
            return dt;
            #endregion

        }

        #endregion
    }
}
